package hw_7_25.generateCaptcha;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class RandomCaptcha {
    static Scanner sc = new Scanner(System.in);
    static Random r = new Random();
    public static void main(String[] args) {

        System.out.println("请输入验证码的位数：");
        int length = sc.nextInt();
        System.out.println("产生的验证码如下：");
        String str = generateStr(length);
        System.out.println(str);
    }
    public static String generateStr(int length){
        List<Character> charList = new ArrayList<>();
        for (char c = '0'; c <= '9'; c++) {
            charList.add(c);
        }
        for (char c = 'a'; c <= 'z'; c++) {
            charList.add(c);
        }
        for (char c = 'A'; c <= 'Z'; c++) {
            charList.add(c);
        }
        int randomIndex = -1;
        StringBuilder codeBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            randomIndex =  r.nextInt(charList.size());
            Character ch = charList.get(randomIndex);
            codeBuilder.append(ch);
        }
        String codeStr = codeBuilder.toString();
        return codeStr;
    }
}
